package lightswitch.server.code.sqlhelper.expression.arithmetic;

import java.util.List;

import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.BinaryExpression;
import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class Division extends BinaryExpression {
public Division(Expression left, Expression right, boolean isNot)
	
	{
		this.leftExpression = left;
		this.rightExpression=right;
		this.isNot=isNot;
		
	}
public Division(String left, String right, boolean isNot, From _from)
{
	leftExpression = ExpressionHelper.parseExpression(left,_from);
	rightExpression = ExpressionHelper.parseExpression(right,_from);
	this.isNot=isNot;
}
public String toString()
{
	return "(" + leftExpression.toString() + " / " + rightExpression.toString();
}
public Object GetValue(DataRow row, List<From> parentFromList) {
	
	Object left = leftExpression.GetValue(row,parentFromList);
	Object right = rightExpression.GetValue(row,parentFromList);

	
	
	String type1 = leftExpression.dataType;
	String type2 = rightExpression.dataType;

	String type = "";
	if (type1.equals(type2)) {
		type = type1;

	} else {
		if ((type1.equals(TypeOfData.INT) && type2.equals(TypeOfData.FLOAT))
				|| (type1.equals(TypeOfData.INT) && type2.equals(TypeOfData.FLOAT))) {
			type = TypeOfData.FLOAT;

		}  else {
			type = TypeOfData.BOOLEAN;
		}
	}
	
	switch(type)
	{
	case "int":
		if(Float.parseFloat(right.toString())==0)
			//throw exception
			return null;
		dataType= TypeOfData.INT;
		return Integer.parseInt(left.toString())/ Integer.parseInt(right.toString());
	case "float":
		if((float)right==0)
			//throw exception
			return null;
		dataType= TypeOfData.FLOAT;
		return Float.parseFloat(left.toString()) / Float.parseFloat(right.toString());

	default:
		//throw exception
		break;
	}
	return null;
}	

}
